package com.childenglish.mapper;

import com.childenglish.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;

@Mapper
public interface SysUserMapper {

    @Select("SELECT u.*, r.role_key as user_role " +
            "FROM sys_user u " +
            "LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id " +  // 修正表连接
            "LEFT JOIN sys_role r ON ur.role_id = r.role_id " +       // 修正表连接
            "WHERE u.user_name = #{username}")
    @Results({
            @Result(property = "id", column = "user_id"),
            @Result(property = "username", column = "user_name"),
            @Result(property = "nickname", column = "nick_name"),
            @Result(property = "phone", column = "phonenumber"),
            @Result(property = "role", column = "user_role"),         // 修正角色字段映射
            @Result(property = "createdTime", column = "create_time")
    })
    SysUser findByUsername(@Param("username") String username);

    @Select("SELECT u.*, r.role_key as user_role " +
            "FROM sys_user u " +
            "LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id " +
            "LEFT JOIN sys_role r ON ur.role_id = r.role_id " +
            "WHERE u.user_id = #{id}")
    @Results({
            @Result(property = "id", column = "user_id"),
            @Result(property = "username", column = "user_name"),
            @Result(property = "nickname", column = "nick_name"),
            @Result(property = "phone", column = "phonenumber"),
            @Result(property = "role", column = "user_role"),
            @Result(property = "createdTime", column = "create_time")
    })
    SysUser findById(@Param("id") Long id);
}